using System;
using System.Collections.Generic;
using System.Windows.Forms;
using WegasManagementSystem.BLL;
using WegasManagementSystem.Models;

namespace WegasManagementSystem.Forms
{
    public partial class EmployeeForm : Form
    {
        private EmployeeBLL employeeBLL;
        private DepartmentBLL departmentBLL;
        private int? editingEmpId = null;

        public EmployeeForm()
        {
            InitializeComponent();
            employeeBLL = new EmployeeBLL();
            departmentBLL = new DepartmentBLL();
        }

        private void EmployeeForm_Load(object sender, EventArgs e)
        {
            // 加载员工列表
            LoadEmployees();

            // 加载部门列表用于下拉选择
            LoadDepartments();

            // 设置性别下拉框
            cboGender.Items.Add("男");
            cboGender.Items.Add("女");
            cboGender.SelectedIndex = 0;
        }

        private void LoadEmployees()
        {
            try
            {
                List<Employee> employees = employeeBLL.GetAllEmployees();
                dgvEmployees.DataSource = employees;
                
                // 显示调整
                dgvEmployees.Columns["EmpId"].HeaderText = "员工ID";
                dgvEmployees.Columns["EmpNumber"].HeaderText = "工号";
                dgvEmployees.Columns["Name"].HeaderText = "姓名";
                dgvEmployees.Columns["Gender"].HeaderText = "性别";
                dgvEmployees.Columns["DeptId"].HeaderText = "部门ID";
                dgvEmployees.Columns["DeptName"].HeaderText = "部门名称";
                dgvEmployees.Columns["Position"].HeaderText = "职务";
                dgvEmployees.Columns["Title"].HeaderText = "职称";
                dgvEmployees.Columns["BaseSalary"].HeaderText = "基本工资";
                dgvEmployees.Columns["CreatedTime"].HeaderText = "创建时间";
                dgvEmployees.Columns["UpdatedTime"].HeaderText = "更新时间";
            }
            catch (Exception ex)
            {
                MessageBox.Show("加载员工列表失败: " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void LoadDepartments()
        {
            try
            {
                List<Department> departments = departmentBLL.GetAllDepartments();
                cboDepartment.DataSource = departments;
                cboDepartment.DisplayMember = "DeptName";
                cboDepartment.ValueMember = "DeptId";
            }
            catch (Exception ex)
            {
                MessageBox.Show("加载部门列表失败: " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            ClearInputs();
            editingEmpId = null;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                // 验证输入
                if (string.IsNullOrEmpty(txtEmpNumber.Text))
                {
                    MessageBox.Show("工号不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                if (string.IsNullOrEmpty(txtName.Text))
                {
                    MessageBox.Show("姓名不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                // 创建员工对象
                Employee employee = new Employee
                {
                    EmpNumber = txtEmpNumber.Text,
                    Name = txtName.Text,
                    Gender = cboGender.SelectedItem.ToString() == "男" ? 1 : 0,
                    DeptId = (int)cboDepartment.SelectedValue,
                    Position = txtPosition.Text,
                    Title = txtTitle.Text,
                    BaseSalary = numSalary.Value
                };

                bool success;
                
                if (editingEmpId.HasValue)
                {
                    // 更新员工
                    employee.EmpId = editingEmpId.Value;
                    success = employeeBLL.UpdateEmployee(employee);
                    if (success)
                    {
                        MessageBox.Show("员工信息更新成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    // 添加新员工
                    int newId = employeeBLL.AddEmployee(employee);
                    success = newId > 0;
                    if (success)
                    {
                        MessageBox.Show("员工添加成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }

                if (success)
                {
                    LoadEmployees();
                    ClearInputs();
                    editingEmpId = null;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存员工信息失败: " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvEmployees.SelectedRows.Count == 0)
            {
                MessageBox.Show("请先选择要删除的员工", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            int empId = (int)dgvEmployees.SelectedRows[0].Cells["EmpId"].Value;
            string empName = dgvEmployees.SelectedRows[0].Cells["Name"].Value.ToString();

            if (MessageBox.Show($"确定要删除员工\"{empName}\"吗？", "确认删除", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                try
                {
                    bool success = employeeBLL.DeleteEmployee(empId);
                    if (success)
                    {
                        MessageBox.Show("员工删除成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        LoadEmployees();
                        ClearInputs();
                        editingEmpId = null;
                    }
                    else
                    {
                        MessageBox.Show("员工删除失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("删除员工失败: " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void dgvEmployees_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvEmployees.SelectedRows.Count > 0)
            {
                // 获取选中的员工ID
                int empId = (int)dgvEmployees.SelectedRows[0].Cells["EmpId"].Value;
                
                // 获取员工详细信息
                Employee employee = employeeBLL.GetEmployeeById(empId);
                
                if (employee != null)
                {
                    // 显示员工信息
                    editingEmpId = employee.EmpId;
                    txtEmpNumber.Text = employee.EmpNumber;
                    txtName.Text = employee.Name;
                    cboGender.SelectedItem = employee.Gender == 1 ? "男" : "女";
                    cboDepartment.SelectedValue = employee.DeptId;
                    txtPosition.Text = employee.Position;
                    txtTitle.Text = employee.Title;
                    numSalary.Value = employee.BaseSalary;
                }
            }
        }

        private void ClearInputs()
        {
            txtEmpNumber.Text = string.Empty;
            txtName.Text = string.Empty;
            cboGender.SelectedIndex = 0;
            if (cboDepartment.Items.Count > 0)
                cboDepartment.SelectedIndex = 0;
            txtPosition.Text = string.Empty;
            txtTitle.Text = string.Empty;
            numSalary.Value = 0;
        }
    }
} 